package test3;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2023-03-07
 * Time: 19:31
 */

//懒汉模式
public class SingletonTest03 {
    public static void main(String[] args) {
        Singleton singleton = Singleton.getInstance();
        Singleton instance = Singleton.getInstance();
        System.out.println(singleton == instance);
    }
}

//这是线程不安全的
class Singleton{
    //静态的 不着急初始化
    private static Singleton instance;

    //外面new不了
    private Singleton(){}

    //提供静态的方法外面拿到
    public static Singleton getInstance(){
        if (instance == null){
            instance = new Singleton();
        }
        return instance;
    }

}